package de.slothsoft.wasbinich.swingclient.common;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.prefs.Preferences;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import de.slothsoft.commons.swing.ButtonType;
import de.slothsoft.commons.swing.GridBagUtil;
import de.slothsoft.commons.swing.PreferenceLoginPanel;
import de.slothsoft.commons.swing.PreferenceLoginPanel.ExtendedUser;
import de.slothsoft.wasbinich.swingclient.res.Messages;

public class LoginDialog extends JDialog {

    private static final long serialVersionUID = -3173964360384481741L;

    private final Preferences preferences;
    protected final ExtendedUser user;

    private JTextField textUsername;
    private JPasswordField textPassword;
    private JCheckBox buttonRememberMe;

    protected ButtonType answer;

    public LoginDialog(Frame parent, Preferences preferences) {
	super(parent, Messages.getString("LoginDialog.title"), true);
	this.preferences = preferences;
	this.user = PreferenceLoginPanel.createDefaultUser(preferences);

	getContentPane().add(createLoginPanel(), BorderLayout.CENTER);
	getContentPane().add(createButtonPanel(), BorderLayout.PAGE_END);
	pack();
	setResizable(false);
	setLocationRelativeTo(parent);
    }

    protected Component createLoginPanel() {
	JPanel panel = new JPanel(new GridBagLayout());
	panel.setBorder(new LineBorder(Color.GRAY));

	int x = 0, y = 0;
	panel.add(new JLabel(Messages.getString("LoginDialog.userName")), GridBagUtil.labelDefaults(x, y++));
	panel.add(new JLabel(Messages.getString("LoginDialog.password")), GridBagUtil.labelDefaults(x, y++));
	panel.add(new JLabel(Messages.getString("LoginDialog.rememberMe")), GridBagUtil.labelDefaults(x, y++));

	x++;
	y = 0;
	this.textUsername = new JTextField();
	this.textUsername.setText(this.user.getLogin());
	panel.add(this.textUsername, GridBagUtil.textFieldDefaults(x, y++));
	this.textPassword = new JPasswordField();
	this.textPassword.setText(this.user.getPassword() == null ? "blaubeere" : new String(this.user.getPassword()));
	this.textPassword.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		performLoginPressed();
	    }
	});
	panel.add(this.textPassword, GridBagUtil.textFieldDefaults(x, y++));
	this.buttonRememberMe = new JCheckBox();
	this.buttonRememberMe.setSelected(this.user.isRemembered());
	panel.add(this.buttonRememberMe, GridBagUtil.textFieldDefaults(x, y++));

	return panel;
    }

    protected Component createButtonPanel() {
	JPanel buttonPanel = new JPanel();

	JButton login = new JButton(Messages.getString("LoginDialog.login"));
	login.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		performLoginPressed();
	    }
	});
	buttonPanel.add(login);
	JButton cancel = new JButton(Messages.getString("LoginDialog.cancel"));
	cancel.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		performCancelPressed();
	    }
	});
	buttonPanel.add(cancel);
	return buttonPanel;
    }

    protected void performLoginPressed() {
	this.user.setLogin(this.textUsername.getText());
	this.user.setPassword(this.textPassword.getPassword());
	this.user.setRemembered(this.buttonRememberMe.isSelected());
	if (this.user.isRemembered()) {
	    PreferenceLoginPanel.storeUser(this.preferences, this.user);
	}
	this.answer = ButtonType.OK;
	performClose();
    }

    protected void performCancelPressed() {
	this.answer = ButtonType.CANCEL;
	performClose();
    }

    protected void performClose() {
	dispose();
    }

    public ExtendedUser getUser() {
	return this.user;
    }

    public ButtonType getAnswer() {
	return this.answer;
    }
}